<?php
/*
 * 车系参数配置类
 * @package    auto
 * @author     YuNian
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 33a7402924e196a2e2617f775f7868247f5a0fe0 $
 */
defined('IN_IXDCW') or exit('Access Denied');
/**
 * Class AutoCarParam
 */
class AutoCarParam extends BasicModule{
	private $_table;

	function __construct() {
		parent::__construct();
		$this->_table = Table::name(Table::NAME_AUTO_MODEL);
	}

	/**
	 * 根据车款id，获取车系下所有车款的数组
	 * @param $modelid
	 * @return array
	 */
	public function getCarModelArr($modelid) {
		$sql = "select arcid from {$this->_table} where amid=$modelid";
		$carid = $this->_db->getVar($sql);
		return $this->getModelArr($carid);
	}

	/**
	 * 根据车系id，获取车系下所有车款的数组
	 * @param $carid
	 * @return array
	 */
	public function getModelArr($carid) {
		$sql = "select amid,name from {$this->_table} where arcid=$carid";
		$modelArr = $this->_db->getAll($sql);

		return $modelArr;
	}




	/**
	 * 获取车款类型
	 * 可能值：passenger commercial rv
	 * @param $modelid
	 * @return null
	 */
	public function getModelType($modelid) {
		$sql = "select modeltype from {$this->_table} where amid=$modelid";
		return $this->_db->getVar($sql);
	}


	/**
	 * 获取私有参数
	 * @param $modelType
	 * @param $modelArr
	 * @return mixed
	 */
	public function getModelShowInfo($modelType,$modelArr) {
		global $modelNameArr;

		//获取到车款的 私有表name
		if($modelType == 'passenger') $table = Table::name(Table::NAME_AUTO_MODEL_PASSENGER);
		if($modelType == 'commercial') $table = Table::name(Table::NAME_AUTO_MODEL_COMMERCIAL);
		if($modelType == 'rv') $table = Table::name(Table::NAME_AUTO_MODEL_RV);
		$modelStr = '';

		//
		foreach($modelArr as $val) {
			$modelStr .= $val['amid'].',';
			$modelNameArr = $val['name'];
		}

		$modelStr = substr($modelStr,0,strlen($modelStr)-1);    //过滤id 后面的点号


		$sql = "select * from $table where amid in($modelStr)  ";
		return $this->_db->getAll($sql);
	}

	/**
	 * 车款的独立参数
	 * $info 写入缓存的参数
	 * @param $name
	 * @param $number
	 * @param $modelType
	 * @return mixed
	 */
	public function getInfo($name,$number,$modelType) {
		global $info;
		if($modelType == 'passenger') $info = PCache::read('fields-auto_model_passenger.php');
		if($modelType == 'commercial') $info = PCache::read('fields-auto_model_commercial.php');
		if($modelType == 'rv') $info = PCache::read('fields-auto_model_rv.php');
		//取出可能值
		foreach($info as $val) {
			if($val['name'] == $name) {
				$strinfo = $val['option_value'];
			}
		}
		$strinfo = explode('*',$strinfo);
		//过滤后的可能值
		foreach($strinfo as $val){
			$arrinfo[] = substr($val,2);
		}
		if($arrinfo[$number] == '请选择') $arrinfo[$number]="未填写";
		if(!$arrinfo[$number]) $arrinfo[$number]="参数溢出";
		return $arrinfo[$number];

	}

	/**
	 * 获取私有参数
	 * @param $modelType
	 * @param $info 写入缓存的参数
	 * @param $modelInfo
	 * @return mixed
	 */


	public function getPrivateType($modelType,$modelInfo) {
		$group = PCache::read('autoCategory-param_group.php');

		//写入缓存的参数
		if($modelType == 'passenger') $infoArr = PCache::read('fields-auto_model_passenger.php');
		if($modelType == 'commercial') $infoArr = PCache::read('fields-auto_model_commercial.php');
		if($modelType == 'rv') $infoArr = PCache::read('fields-auto_model_rv.php');

		//乘用车的私有字段id arrchildid
		if($modelType == 'passenger') {
			foreach($group as $val) {
				if($val['name'] == '乘用车') $arrchildid = $val['arrchildid'];
			}
		}
		//商用车的私有字段id arrchildid
		if($modelType == 'commercial') {
			foreach($group as $val) {
				if($val['name'] == '商用车') $arrchildid = $val['arrchildid'];
			}
		}
		//房车的私有字段id arrchildid
		if($modelType == 'rv') {
			foreach($group as $val) {
				if($val['name'] == '房车') $arrchildid = $val['arrchildid'];
			}
		}

		$arrchildid = explode(',',$arrchildid);
		foreach($group as $val) {
			foreach($arrchildid as  $v) {
				if($val['arid']==$v && $val['level'] == 1) {
					$arr[] = $val;
				}
			}
		}



		//整合 类别，属性名称，属性值
		foreach($arr as $type) {
			foreach($infoArr as $modelExtName) {

				if($modelExtName['param_group'] == $type['arid']) {
					if($modelExtName['html'] == 'select') {

						foreach($modelInfo as $modelExt){
							$carParam[$type['name']][$modelExtName['title']][] = $this->getInfo($modelExtName['name'],$modelExt[$modelExtName['name']],$modelType);
						}
					}else{
						foreach($modelInfo as $modelExt){
							$carParam[$type['name']][$modelExtName['title']][] = $modelExt[$modelExtName['name']];
						}
					}
				}
//
			}
		}
		return $carParam;
	}

}